package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        shopMapper.patchDel(ids);
    }

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1.校验
        //1.1 空校验
        //1.2 用户是否被注册 如果被注册？？ 店铺已经注册，请直接登录，如果忘记密码，请找回密码
        Employee admin = shop.getAdmin();
        Employee employeeObj = employeeMapper.loadByUser(admin);
        if(employeeObj != null){
            throw new BusinessException("店铺已经注册，请直接登录，如果忘记密码，请找回密码!!");
        }
        //2.入驻
        // 2.1 先保存管理员信息 （需要返回自增id）
        employeeService.add(admin);
        // 2.2 保存shop
        shop.setAdmin(admin);
        shop.setState(1);//默认直接通过可用
        shopMapper.save(shop);
        //================2.3需要重新修改员工信息，绑定店铺
        admin.setShop(shop);
        employeeMapper.update(admin);

        //@TODO 3.（由于我们是默认通过）发短信或者邮件通知admin 可以使用店铺了？？
    }
}
